package info.johnwheeler.gaestorage.servlet;

import info.johnwheeler.gaestorage.core.County;
import info.johnwheeler.gaestorage.core.CountyDao;
import info.johnwheeler.gaestorage.core.Document;
import info.johnwheeler.gaestorage.core.Person;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import com.googlecode.objectify.Key;

public class CountyServlet extends AbstractUploadServlet {
    private CountyDao dao = new CountyDao();    

    @Override
    protected void showForm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map counties = dao.findAll();
        req.setAttribute("counties", counties);
        //PersonDao personDao = new PersonDao();
        //ArrayList<Person> people = personDao.findAll();   
        //req.setAttribute("people", people);
    	req.getRequestDispatcher("county.jsp").forward(req, resp);        
    }

    @Override
    protected void handleSubmit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletFileUpload upload = new ServletFileUpload();

        try {
            FileItemIterator it = upload.getItemIterator(req);

            County county = new County();

            while (it.hasNext()) {
                FileItemStream item = it.next();
                String fieldName = item.getFieldName();
                InputStream fieldValue = item.openStream();

                if ("name".equals(fieldName)) {
                    county.setName(Streams.asString(fieldValue));
                    continue;
                }

                if ("address".equals(fieldName)) {
                	county.setAddress(Streams.asString(fieldValue));
                    continue;
                }
                
                if("city".equals(fieldName)){
                	county.setCity(Streams.asString(fieldValue));
                	continue;
                }
                
                if("state".equals(fieldValue)){
                	county.setState(Streams.asString(fieldValue));
                	continue;
                }
                
                if("country".equals(fieldValue)){
                	county.setCountry(Streams.asString(fieldValue));
                	continue;
                }
                
                if("owner".equals(fieldName)){
                	int id = (int) Long.parseLong(Streams.asString(fieldValue));
                	Key<Person> key = new Key<Person>(Person.class, id);
                	county.setOwner(key);
                	continue;
                }
                
            }

            dao.save(county);
            resp.sendRedirect("county?action=display&id=" + county.getId());            
        } catch (FileUploadException e) {
            throw new ServletException(e);
        }      
    }

    @Override
    protected void showRecord(long id, HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        /*
    	Document document = dao.findById(id);
        
        resp.setContentType(document.getContentType());        
        resp.getOutputStream().write(document.getDocumentData());
        resp.flushBuffer();
        */                    
    }
}
